package com.panda.QQserver.service;

import com.panda.qqcommon.Message;
import com.panda.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

public class ServerConnectClientThread extends Thread{
    private Socket socket;
    private String userId;

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public ServerConnectClientThread(Socket socket, String userId){
        this.socket=socket;
        this.userId=userId;
    }

    @Override
    public void run(){
        while (true){
            System.out.println("服务端与用户 "+userId+" 保持通讯");
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                Message message=(Message)objectInputStream.readObject();

                //接收客户端发送的message，根据信息类型进行各种处理
                if(message.getMessageType().equals(MessageType.MESSAGE_GET_ONLINE_LIST)){
                    //当客户端申请要在线用户列表

                    //创建message
                    Message message1 = new Message();
                    message1.setMessageType(MessageType.MESSAGE_RETURN_ONLINE_LIST);
                    message1.setSender(message.getSender());
                    message1.setContent(ManageClientThreads.getOnlineUsers());

                    //输出流
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    objectOutputStream.writeObject(message1);


                }else if(message.getMessageType().equals(MessageType.MESSAGE_CLIENT_EXIT)){
                    //退出线程
                    System.out.println("exit the thread:"+userId);
                    ManageClientThreads.removeThread(userId);
                    socket.close();
                    break;

                }else if(message.getMessageType().equals(MessageType.MESSAGE_COMMON_MESSAGE)){

                    if (ManageClientThreads.getHm().containsKey(message.getGetter())) {
                        //如果接受者在线的话就发送
                        ServerConnectClientThread thread = ManageClientThreads.getThread(message.getGetter());
                        ObjectOutputStream objectOutputStream = new ObjectOutputStream(thread.socket.getOutputStream());
                        objectOutputStream.writeObject(message);
                    }else {
                        //如果不在线，就启动一个监视线程，不断在ManageClientThreads中查验是否该用户登录，如果登录马上发消息
                        MonitorIsOnline monitorIsOnline = new MonitorIsOnline();
                        monitorIsOnline.message=message;
                        monitorIsOnline.start();
                    }

                }else if(message.getMessageType().equals(MessageType.MESSAGE_TO_ALL)){
                    HashMap<String,ServerConnectClientThread> hm = ManageClientThreads.getHm();
                    Iterator iterator = hm.keySet().iterator();
                    while(iterator.hasNext()){
                        String userId = (String)iterator.next();
                        if(!userId.equals(message.getSender())){
                            ObjectOutputStream objectOutputStream = new ObjectOutputStream(hm.get(userId).getSocket().getOutputStream());
                            objectOutputStream.writeObject(message);
                        }

                    }
                }


                else {
                    //其他的类型，暂时不处理。
                }





            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
